<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="源单编号" prop="sourceCode">
        <el-input
          v-model="queryParams.sourceCode"
          placeholder="请输入源单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="cgrkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="入库编号" align="center" prop="stockInCode"/>

      <el-table-column label="订单编号" align="center" prop="orderCode"/>
      <el-table-column label="入库类型" align="center" prop="typeName"/>
      <el-table-column label="供应商" align="center" prop="supName"/>
      <el-table-column label="结算金额" align="center" prop="cost"/>
      <el-table-column label="已结算金额" align="center" prop="paidAmount"/>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购应付对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入单据编号"/>
        </el-form-item>
        <el-form-item label="源单编号" prop="sourceCode">
          <el-input v-model="form.sourceCode" placeholder="请输入源单编号"/>
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额"/>
        </el-form-item>
        <el-form-item label="已核销金额" prop="verifiedAmount">
          <el-input v-model="form.verifiedAmount" placeholder="请输入已核销金额"/>
        </el-form-item>
        <el-form-item label="单据日期" prop="docTime">
          <el-date-picker clearable
                          v-model="form.docTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择单据日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="单据阶段" prop="docStage">
          <el-input v-model="form.docStage" placeholder="请输入单据阶段"/>
        </el-form-item>
        <el-form-item label="制单人" prop="docMakeUserId">
          <el-input v-model="form.docMakeUserId" placeholder="请输入制单人"/>
        </el-form-item>
        <el-form-item label="供应商id" prop="supId">
          <el-input v-model="form.supId" placeholder="请输入供应商id"/>
        </el-form-item>
        <el-form-item label="业务员id" prop="businessUserId">
          <el-input v-model="form.businessUserId" placeholder="请输入业务员id"/>
        </el-form-item>
        <el-form-item label="审批结果" prop="approvalResult">
          <el-input v-model="form.approvalResult" placeholder="请输入审批结果"/>
        </el-form-item>
        <el-form-item label="审批意见" prop="approvalRemark">
          <el-input v-model="form.approvalRemark" placeholder="请输入审批意见"/>
        </el-form-item>
        <el-form-item label="审批人id" prop="approvalUserId">
          <el-input v-model="form.approvalUserId" placeholder="请输入审批人id"/>
        </el-form-item>
        <el-form-item label="是否生效" prop="isEffect">
          <el-input v-model="form.isEffect" placeholder="请输入是否生效"/>
        </el-form-item>
        <el-form-item label="是否关闭" prop="isClose">
          <el-input v-model="form.isClose" placeholder="请输入是否关闭"/>
        </el-form-item>
        <el-form-item label="是否作废" prop="isCancel">
          <el-input v-model="form.isCancel" placeholder="请输入是否作废"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listBuyMustPay, getBuyMustPay, delBuyMustPay, addBuyMustPay, updateBuyMustPay} from "@/api/finance/buyMustPay";
import {listStockInPlus} from "@/api/store/stockIn";

export default {
  name: "SelectYfxqd",
  props:['supId'],
  data() {
    return {
      // 被选中的应付需求单列表
      selectYfxqdList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购应付表格数据
      cgrkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        sourceCode: null,
        docTime: null,
        docStage: null,
        docMakeUserId: null,
        supId: this.supId,
        businessUserId: null,
        approvalResult: null,
        approvalRemark: null,
        approvalUserId: null,
        isEffect: null,
        isClose: null,
        isCancel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          {required: true, message: "单据编号不能为空", trigger: "blur"}
        ],
        docTime: [
          {required: true, message: "单据日期不能为空", trigger: "blur"}
        ],
      }
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    setSupId(supId) {
      // alert(supId)
      this.queryParams.supId = supId;
      this.getList();
    },
    returnSelectCgrkList() {
      return this.selectYfxqdList;
    },
    /** 查询采购应付列表 */
    getList() {
      this.loading = true;
      listStockInPlus(this.queryParams).then(response => {
        this.cgrkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        sourceCode: null,
        totalAmount: null,
        verifiedAmount: null,
        docTime: null,
        docStage: null,
        docMakeUserId: null,
        supId: null,
        businessUserId: null,
        approvalResult: null,
        approvalRemark: null,
        approvalUserId: null,
        isEffect: null,
        isClose: null,
        isCancel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.selectYfxqdList = selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购应付";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBuyMustPay(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购应付";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBuyMustPay(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuyMustPay(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除采购应付编号为"' + ids + '"的数据项？').then(function () {
        return delBuyMustPay(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('finance/buyMustPay/export', {
        ...this.queryParams
      }, `buyMustPay_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
